package com.ddx.controller.core;

import com.ddx.common.exception.ExceptionUtil;
import com.ddx.common.exception.ResultModel;
import com.ddx.pojo.ShortVideo;
import com.ddx.pojo.ShortVideoComment;
import com.ddx.pojo.ShortVideoComplaint;
import com.ddx.pojo.ShortVideoLike;
import com.ddx.service.ICommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: 评论
 * @date 2019/1/13
 */
@RestController
@RequestMapping(value = "/app")
public class CommentController {
    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    ICommentService iCommentService;

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/comment/commentList")
    public ResultModel commentList(HttpServletRequest request, @RequestBody Map<String,Object> map) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            String userId = request.getHeader("userId");
            int videoId = Integer.parseInt(map.get("videoId").toString());
            Map<String,Object> objectMap=iCommentService.commentList(videoId,userId);
            resultModel.setData(objectMap);
        } catch (Exception e) {
            logger.error("查询评论列表 fail{}", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/comment/uploadComment")
    public ResultModel uploadComment(HttpServletRequest request, @RequestBody ShortVideoComment shortVideoComment) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            shortVideoComment.setUserId(userId);
            iCommentService.uploadComment(shortVideoComment);
        } catch (Exception e) {
            logger.error("上传评论 fail{}", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/comment/commentThumbsUp")
    public ResultModel commentThumbsUp(HttpServletRequest request, @RequestBody ShortVideoComment shortVideoComment) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            shortVideoComment.setUserId(userId);
            iCommentService.commentThumbsUp(shortVideoComment);
        } catch (Exception e) {
            logger.error("评论点赞 fail{}", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/comment/videoThumbsUp")
    public ResultModel videoThumbsUp(HttpServletRequest request, @RequestBody ShortVideo shortVideo) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            shortVideo.setUserId(userId);
            iCommentService.videoThumbsUp(shortVideo);
        } catch (Exception e) {
            logger.error("视频点赞 fail{}", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/news/newsRecord")
    public ResultModel newsRecord(HttpServletRequest request) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            Integer userId = Integer.parseInt(request.getHeader("userId"));
            Map<String,Object> map=iCommentService.newsRecord(userId);
            resultModel.setData(map);
        } catch (Exception e) {
            logger.error("消息记录 fail{}", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/video/complaint")
    public ResultModel complaint(HttpServletRequest request, @RequestBody ShortVideoComplaint shortVideoComplaint) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            Integer userId = Integer.parseInt(request.getHeader("userId"));
            shortVideoComplaint.setUserId(userId);
            iCommentService.complaint(shortVideoComplaint);
        } catch (Exception e) {
            logger.error("小视频举报 fail{}", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/video/myComplaint")
    public ResultModel myComplaint(HttpServletRequest request) {
        ResultModel resultModel=ResultModel.getSuccessInstance();
        try {
            Integer userId = Integer.parseInt(request.getHeader("userId"));
            Map<String,Object> map=iCommentService.myComplaint(userId);
            resultModel.setData(map);
        } catch (Exception e) {
            logger.error("我的小视频举报 fail{}", e);
            resultModel= ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
}
